package com.campus.controller;

import com.campus.entry.SysUser;
import com.campus.exception.BusinessException;
import com.campus.pojo.dto.SysUserDto;
import com.campus.response.ResponseResult;
import com.campus.response.ResultCode;
import com.campus.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 新增用户
     * @param sysUser
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'root'})")
    @PostMapping("/save")
    public ResponseResult<Object> saveSysUser(@RequestBody SysUser sysUser){
        if (sysUser.getPassword()==null || sysUser.getLoginName()==null){
            throw new BusinessException(ResultCode.ERROR);
        }
        sysUserService.saveSysUser(sysUser);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 根据用户Id查询用户
     * @param userId
     * @return {@link ResponseResult }<{@link SysUser }>
     */
    @PreAuthorize("hasAnyRole({'root'})")
    @GetMapping("/getById")
    public ResponseResult<SysUser> getById(@RequestParam Integer userId){
        if (Objects.isNull(userId)){
            throw new BusinessException(ResultCode.ERROR);
        }
        SysUser sysUserById = sysUserService.getSysUserById(userId);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,sysUserById);

    }

    /**
     * 根据用户名查询用户
     * @param loginName
     * @return {@link ResponseResult }<{@link SysUser }>
     */
    @PreAuthorize("hasAnyRole({'root'})")
    @GetMapping("/getByName")
    public ResponseResult<SysUser> getByName(@RequestParam String loginName){
        if (Objects.isNull(loginName)){
            throw new BusinessException(ResultCode.ERROR);
        }
        SysUser sysUserByName = sysUserService.getSysUserByName(loginName);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,sysUserByName);
    }

    /**
     * 获取用户列表
     * @return {@link ResponseResult }<{@link List }<{@link SysUserDto }>>
     */
    @PreAuthorize("hasAnyRole({'root'})")
    @GetMapping("/listSysUser")
    public ResponseResult<List<SysUserDto>> listSysUser(){
        List<SysUserDto> sysUsers=sysUserService.listSysUser();
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,sysUsers);
    }

    /**
     * 更新用户
     * @param sysUser
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PutMapping("/update")
    public ResponseResult<Object> updateUser(@RequestBody SysUser sysUser){
        sysUserService.updateSysUser(sysUser);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return {@link ResponseResult }<{@link Object }>
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult<Object> removeSysUserById(@PathVariable Integer id){
        sysUserService.updateState("2",id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }
}
